<?php
namespace app\admin\controller;
use think\Db;
class Product extends Base{
	public function product_list(){
		$key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
	        $list=Db::name('product')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->field('a.*,b.cname')->where('a.status','<',"3")->where('a.time','>',"{$fromtime}")->where('a.time','<',"{$totime}")->where('title|cname','like',"%{$key}%")->order('a.id desc')->paginate(10);
	        $lists=Db::name('product')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->field('a.*,b.cname')->where('a.status','<',"3")->where('a.time','>',"{$fromtime}")->where('a.time','<',"{$totime}")->where('title|cname','like',"%{$key}%")->select();
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
	}
    public function product_add(){
    	if(request()->isGet()){
            $customers=Db::name('customer')->where('status',"1")->select();
            $this->assign('customers',$customers);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['title']=$param['title'];
            $data['cus_id']=$param['cus_id'];
            $data['totalmoney']=$param['totalmoney'];
            $data['sharemoney']=$param['totalmoney'];
			$data['moneyf']=$param['moneyf'];
			$data['moneyt']=$param['moneyt'];
			$data['cycle']=$param['cycle'];
			$data['detail']=$param['detail'];
			$data['time']=time();
			$data['status']=1;
			$res=Db::name('product')->insert($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '调试']);
    	}
    }
    public function product_edit(){
    	if(request()->isGet()){
			$param = input('get.');
			$user=Db::name('product')->where('id',$param['id'])->find();
            $customers=Db::name('customer')->where('status',"1")->select();
            $this->assign('customers',$customers);
	        $this->assign('user',$user);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['title']=$param['title'];
            $data['cus_id']=$param['cus_id'];
            $data['totalmoney']=$param['totalmoney'];
            $data['sharemoney']=$param['sharemoney'];
			$data['moneyf']=$param['moneyf'];
			$data['moneyt']=$param['moneyt'];
			$data['cycle']=$param['cycle'];
			$data['detail']=$param['detail'];
			$data['time']=time();
			$data['status']=1;
        	$res= Db::name('product')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '更新成功']);
    	}
    }
    public function state_off(){
    	$id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
    	$id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已开启']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function product_del(){
		$param = input('post.');
        $res= Db::name('product')->where(array('id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
    public function product_dels(){
		$param = input('post.');
        $res= Db::name('product')->where(array('id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
}